/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Utilidades;

/**
 *
 * @author santiago
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Conexion {
 
    public Connection Con;
    /**
     * Metodo que sirve para obtener una conexion
     * @return con
     */
    public Connection getCon(){
       ConexionBD();
        return Con;
    }
    /**
     * Metodo que sirve para establecer una conexion
     * @param Con Conexion
     */
    public void setCon(Connection Con){
        this.Con = Con;
    }

static
{
    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    
    }catch(ClassNotFoundException e1){
            System.out.print("ClassNotFoundException"+ e1.getMessage());
        }
    catch(InstantiationException e2){
          System.out.print("InstantiationException"+e2.getMessage());       
        }
        
    catch(IllegalAccessException e3){
        System.out.print("IllegalAccessException"+e3.getMessage());
    }
    catch(Exception e4){
        System.out.print("Exception"+e4.getMessage());
    }
}

/**
 * Metodo que sirve para conectarse a la base de datos
 */
public void ConexionBD(){
  try {
        setCon(DriverManager.getConnection("jdbc:mysql://localhost:3306/estrellate","root",""));
  }
  catch(SQLException e5) {
       System.out.print("SQLException"+e5.getMessage());
    }
  catch(Exception e6){
      System.out.print("Exception"+e6.getMessage());
  }
}

public static void main(String[] args){
    Conexion Connection=new Conexion();
    //para comprobar si hay usuarios registrados
    try{
       ResultSet  r=Connection.getCon().prepareStatement("select * from usuario").executeQuery();  
       if(r.next()){
           System.out.println("usuario"+" "+r.getString(1)+"Nombre:"+" "+r.getString(2));
           while(r.next()){
               System.out.println("usuario"+" "+r.getString(1)+"Nombre:"+" "+r.getString(2));
           }
         }
       else {
              System.out.println("no hay datos");
             }
        }
        catch(Exception e){
           System.out.println("Exception"+e.getMessage());
    }
    finally{
         
        }
}

}
